<?php
session_start();
require_once "../includes/common.php";
require_once BASE_PATH.'/class/Explore.class.php';
require_once BASE_PATH.'/class/Sys.class.php';

class AiApi {
    private $explore;
    private $sys;


    public function __construct() {
        $this->explore = new Explore();
        $this->sys = new Sys();
    }

    public function handleRequest() {
        if (!isset($_GET['type'])) {
            $this->respond(400);
            return;
        }

        switch ($_GET['type']) {
            case 'aicheck':
                $this->aicheck();
                break;

            default:
                $this->respond(400);
                break;
        }
    }

    private function aicheck() {
        $ai_check_status = json_decode($this->sys->SysConfig(),true)['ai_check'];
        if($ai_check_status!=1){
            return $this->respond("AI审核未开启！");
        }

        $assistant_id = "xxx";                 // AI审核文章笔记BOT的智能体ID
        $message = $this->explore->getAIformatCheckNotes();
        $token = "xxx";              // AI审核文章笔记BOT的Token

        $note_title = json_decode($message,true)[0]['note_title'];

        if ($message=='null'){
            return $this->respond("已经没有要审核的内容了！-不执行ai api");
        }
        //var_dump(gettype($message));exit;
        $response = json_decode(callTencentAiApi($assistant_id, $token, $message),true);


        if($response["error"]){
            return $this->respond('腾讯API返回error：'.$response["error"]['message']);
        }else{
            $aires = json_decode($response["choices"][0]["message"]["content"],true);

            $ainid = $aires['note_id'];
            $adminuid = "310da744a39058d3076ea7eb";
            $aicheck_res = $aires['check_result'];

            $chang_check = json_decode($this->explore->NoteChangeok($ainid,$adminuid,$aicheck_res),true);
            //var_dump($chang_check['message'].'-'.$aicheck_res);
            if ($aicheck_res == 1) {
                $cnMessage = "AI审核结果：通过";
            } elseif ($aicheck_res == 2) {
                $cnMessage = "AI审核结果：违规";
            } else {
                $cnMessage = "AI审核结果：未知";
            }
            return $this->respond("笔记：《{$note_title}》".$chang_check['message'].'-'.$cnMessage);
        }


    }


    private function respond($data) {
        header('Content-Type: application/json');
        echo $data;
        exit;
    }
}

$api = new AiApi();
$api->handleRequest();


